<?php namespace App\Http\Controllers\Api;

use App\DeviceData;
use App\Devices;
use App\Http\Controllers\Controller;
use App\Http\Services\UserManager;
use App\User;
use App\UserDevices;
use App\UserWater;
use Illuminate\Http\Request;

class DeviceController extends Controller
{
    public function getDeviceData(Request $request){
        $deviceNum = $request->get("device_num");
        $lastNum   = $request->get("last_num");
        $type      = $request->get("type");

        $newDeviceData = new DeviceData();
        $newDeviceData->device_num = $deviceNum;
        $newDeviceData->last_num   = $lastNum;
        $newDeviceData->save();

        $allDevices = UserDevices::whereDeviceNum($deviceNum)->get();
        if(count($allDevices) > 0){
            foreach ($allDevices as $allDevice){
                if($type == "online"){
                    $allDevice->is_online = 1;
                    $allDevice->save();
                }elseif ($type == "first_data"){
                    $allDevice->is_online = 1;
                    $allDevice->peel = -$lastNum;
                    $allDevice->save();
                }else{
                    $allDevice->is_online = 1;
                    $allDevice->last_num = round(abs(abs($lastNum) - $allDevice->peel) * 5,2);
                    $allDevice->save();
                }

            }
        }

        \Log::error("deviceNum==>".$deviceNum);
        \Log::error("lastNum==>".$lastNum);
        \Log::error("type==>".$type);

        return "complete";
    }

    public function getDeviceLastData(Request $request){
        /** @var User $user */
        $user = UserManager::isLogin($request);
        if (!$user)
            return response()->json(['level' => 'error','message' => '请先登录!','data' => 'no_login']);

        $deviceNum = $request->get("device_num");

        $thatLastData  = UserDevices::whereDeviceNum($deviceNum)->whereUserId($user->id)->first();
        $dataRatio = 0;
        $dataKg = 0;
        if($thatLastData){
            if(strtotime($thatLastData->updated_at) < time() - 30){
                $thatLastData->is_online = 0;
                $thatLastData->save();
            }

            if($thatLastData->last_num > $thatLastData->max_num){
                $thatLastData->max_num = $thatLastData->last_num;
                $thatLastData->save();
            }

            $dataKg = round($thatLastData->last_num / 1000 , 2);
            if($thatLastData->max_num != 0){
                $dataRatio = round($thatLastData->last_num / $thatLastData->max_num,2) * 100;
            }else{
                $dataRatio=0;
            }
        }

        $userWater = UserWater::whereUserId($user->id)->first();

        $result = [
            "data_ratio" => round($dataRatio,2),
            "data_kg"    => round($dataKg,2),
            "is_online"  => $thatLastData->is_online,
            "stock_num"  => $userWater ? $userWater->water_stock_num : 0,
            "created_at" => date("Y-m-d H:i:s",strtotime($thatLastData->created_at))
        ];

        return response()->json(["level"=>"success","message"=>"获取成功","data"=>$result]);
    }

    public function myDeviceList(Request $request){
        /** @var User $user */
        $user = UserManager::isLogin($request);
        if (!$user)
            return response()->json(['level' => 'error','message' => '请先登录!','data' => 'no_login']);

        $thatUserDevices = UserDevices::whereUserId($user->id)->get();
        if(count($thatUserDevices) > 0){
            foreach ($thatUserDevices as $thatUserDevice){
                $thatUserDevice->device     = Devices::whereDeviceNum($thatUserDevice->device_num)->first();
                if($thatUserDevice->max_num == 0){
                    $thatUserDevice->last_point = "0%";
                }else{
                    $thatUserDevice->last_point = round(abs($thatUserDevice->last_num - $thatUserDevice->peel) / $thatUserDevice->max_num,2) * 100 . "%";
                }

            }
        }

        $device_count = UserDevices::whereUserId($user->id)->count();
        $result = [
            "device_list"  => $thatUserDevices,
            "device_count" => $device_count
        ];

        return response()->json(["level"=>"success","message"=>"获取成功","data"=>$result]);
    }

    public function addDevice(Request $request){
        /** @var User $user */
        $user = UserManager::isLogin($request);
        if (!$user)
            return response()->json(['level' => 'error','message' => '请先登录!','data' => 'no_login']);

        $deviceNum = $request->get("device_num");

        $thatDevice = Devices::whereDeviceNum($deviceNum)->first();
        if(!$thatDevice){
            return response()->json(["level"=>"success","message"=>"设备号不存在"]);
        }

        $thatMyDevice = UserDevices::whereUserId($user->id)->whereDeviceNum($deviceNum)->first();
        if($thatMyDevice){
            return response()->json(["level"=>"success","message"=>"您已经添加了该设备！"]);
        }

        $thatMyDevice = new UserDevices();
        $thatMyDevice->user_id = $user->id;
        $thatMyDevice->device_num = $thatDevice->device_num;
        $thatMyDevice->device_name = $thatDevice->device_name;
        $thatMyDevice->device_type = 1;
        $thatMyDevice->save();

        return response()->json(["level"=>"success","message"=>"添加成功","data"=>$thatMyDevice]);
    }

    public function getDeviceInfo(Request $request){
        /** @var User $user */
        $user = UserManager::isLogin($request);
        if (!$user)
            return response()->json(['level' => 'error','message' => '请先登录!','data' => 'no_login']);

        $deviceNum = $request->get("device_num");

        $thatMyDevice = UserDevices::whereUserId($user->id)->whereDeviceNum($deviceNum)->first();
        if(!$thatMyDevice){
            return response()->json(['level' => 'error','message' => '设备不存在!']);
        }


    }

    public function resetPeel(Request $request){
        /** @var User $user */
        $user = UserManager::isLogin($request);
        if (!$user)
            return response()->json(['level' => 'error','message' => '请先登录!','data' => 'no_login']);

        $deviceNum = $request->get("device_num");

        $thatMyDevice = UserDevices::whereUserId($user->id)->whereDeviceNum($deviceNum)->first();
        if(!$thatMyDevice){
            return response()->json(['level' => 'error','message' => '设备不存在!']);
        }

        $thatDeviceData = DeviceData::whereDeviceNum($deviceNum)->orderBy("id","desc")->first();
        $lastData = 0;
        if($thatDeviceData){
            $lastData = - $thatDeviceData->last_num;
        }
        $thatMyDevice->peel = $lastData;
        $thatMyDevice->save();

        return response()->json(['level' => 'success','message' => '重置成功!']);
    }

    public function setDeviceMaxNum(Request $request){
        /** @var User $user */
        $user = UserManager::isLogin($request);
        if (!$user)
            return response()->json(['level' => 'error','message' => '请先登录!','data' => 'no_login']);

        $deviceNum = $request->get("device_num");
        $maxNum = $request->get("max_num");

        $thatMyDevice = UserDevices::whereUserId($user->id)->whereDeviceNum($deviceNum)->first();
        if(!$thatMyDevice){
            return response()->json(['level' => 'error','message' => '设备不存在!']);
        }

        if(is_null($maxNum) || $maxNum == ''){
            return response()->json(['level' => 'error','message' => '重量不能为空!']);
        }

        $thatMyDevice->max_num = $maxNum * 1000;
        $thatMyDevice->save();
        return response()->json(['level' => 'success','message' => '设置成功!']);

    }

    public function changeStockNum(Request $request){
        /** @var User $user */
        $user = UserManager::isLogin($request);
        if (!$user)
            return response()->json(['level' => 'error','message' => '请先登录!','data' => 'no_login']);

        $deviceNum = $request->get("device_num");

        $thatUserWater = UserWater::whereUserId($user->id)->first();
        if(!$thatUserWater){
            return response()->json(['level' => 'error','message' => '设备不存在!']);
        }

        if($thatUserWater->water_stock_num > 1){
            $thatUserWater->water_stock_num = $thatUserWater->water_stock_num - 1;
            $thatUserWater->save();
        }else{
            return response()->json(['level' => 'error','message' => '存余水不足!',"data"=>"no_stock"]);
        }

        return response()->json(['level' => 'success','message' => '换水成功!']);

    }
}
